import message from "./message.js";

export default {
	
  goLogin: function() {
    wx.navigateTo({
      url: "../login/login"
    });
  },

  time: function(date1) {
    date1 = String(date1);
    date1 = new Date(Date.parse(date1.replace(/-/g, "/"))).getTime();
    var date2 = new Date();
    date2 = String(date2);
    date2 = new Date(Date.parse(date2.replace(/-/g, "/"))).getTime();
    var date3 = new Date(date2).getTime() - new Date(date1).getTime(); //时间差的毫秒数
    //------------------------------

    //计算出相差天数
    var days = Math.floor(date3 / (24 * 3600 * 1000));

    //计算出小时数

    var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000));
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000));
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000);
    // alert(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟")
    if (days == 0 && hours == 0) {
      return minutes;
    } else if (days == 0 && hours != 0) {
      return hours * 60 + minutes + "分前";
    } else if (days != 0) {
      return days + "天内";
    }
  },

  // 思路：获取没重复的最右一值放入新数组
  /*
   * 推荐的方法
   *
   * 方法的实现代码相当酷炫，
   * 实现思路：获取没重复的最右一值放入新数组。
   * （检测到有重复值时终止当前循环同时进入顶层循环的下一轮判断）*/
  uniq(array) {
    var temp = [];
    var index = [];
    var l = array.length;
    for (var i = 0; i < l; i++) {
      for (var j = i + 1; j < l; j++) {
        if (array[i] === array[j]) {
          i++;
          j = i;
        }
      }
      temp.push(array[i]);
      index.push(i);
    }
    return temp;
  },

  hideWord(str) {
    let string = String(str);
    let len = string.length;
    let stringBefore = string.slice(0, 5);
    let stringBeHand = string.slice(len - 10, len + 1);
    let newString = stringBefore + "..." + stringBeHand;
    return newString;
  },
  replaceAll(ori, s1, s2) {　　
    return ori.replace(new RegExp(s1, "gm"), s2);　　
  },
  errHandler(res) {
    if (res.data.title == 'Unauthorized') {
      uni.redirectTo({
        url: '/pages/login/login'
      })
      return
    } else {
      message.showInfo(
        res.data.message ? res.data.message : "网络连接异常"
      );
			return true;
    }
  }
};